package com.derekandbritt.booST.preferences;

import com.derekandbritt.booST.R;
import com.derekandbritt.booST.booSTActivity;
import com.derekandbritt.booST.exceptions.ConnectionException;
import com.derekandbritt.booST.exceptions.DataFormatException;
import com.derekandbritt.booST.gps.GPSService;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ToggleButton;

public class PreferencesActivity extends booSTActivity {

	private Intent gpsIntent;

	private ToggleButton kokoToggle;
	private ToggleButton gpsToggle;
	private EditText booSTServerField;
	private EditText kokoServerField;
	
	@Override
	protected void displayContent() throws ConnectionException, DataFormatException {
        
        //draw the screen
        setContentView(R.layout.preferences);
        
        //initialize intents
        gpsIntent = new Intent(this, GPSService.class);
        
        //initialize elements
        kokoToggle = (ToggleButton) findViewById(R.id.KokoToggle);
        gpsToggle = (ToggleButton) findViewById(R.id.GPSToggle);
        booSTServerField = (EditText) findViewById(R.id.booSTServer);
        kokoServerField = (EditText) findViewById(R.id.kokoServer);
        
        //initialize values
        SharedPreferences settings = getSharedPreferences(Preferences.BOOST_PREFS, 0);
        kokoToggle.setChecked(settings.getBoolean(Preferences.KOKO_TRAINING_ENABLED_PREF, false));
        gpsToggle.setChecked(settings.getBoolean(Preferences.GPS_ENABLED_PREF, false));
        booSTServerField.setText(settings.getString(Preferences.BOOST_SERVER, ""));
        kokoServerField.setText(settings.getString(Preferences.KOKO_SERVER, ""));
        
        //setup listeners
        setupKokoListeners();
        setupGPSListeners();
        setupServerListeners();     
    }

    private void setupKokoListeners() {
        kokoToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton compoundButton, boolean flag) {
                
                SharedPreferences settings = getSharedPreferences(Preferences.BOOST_PREFS, 0);
                SharedPreferences.Editor editor = settings.edit();
                
                if(compoundButton.getId() == kokoToggle.getId()) {
                    editor.putBoolean(Preferences.KOKO_TRAINING_ENABLED_PREF, flag);
                    trainingMode = true;
                }
                
                editor.commit();
            }
        });
    }
	
    private void setupGPSListeners() {
    	gpsToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton compoundButton, boolean flag) {
				
				SharedPreferences settings = getSharedPreferences(Preferences.BOOST_PREFS, 0);
			    SharedPreferences.Editor editor = settings.edit();
			    
			    if(compoundButton.getId() == gpsToggle.getId()) {
			    	editor.putBoolean(Preferences.GPS_ENABLED_PREF, flag);
			    	if(flag) {
			    		startService(gpsIntent);
			    	}
			    	else {
			    		stopService(gpsIntent);
			    	}
			    }
			    
			    editor.commit();
			}
        });
    }
    
    private void setupServerListeners() {
    	
    	Button saveButton = (Button)findViewById(R.id.ServerSaveButton);
    	Button cancelButton = (Button)findViewById(R.id.ServerCancelButton);
    	Button testButton = (Button)findViewById(R.id.ServerTestButton);
    	
    	saveButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				String booST = booSTServerField.getText().toString();
				String koko = kokoServerField.getText().toString();
				
				SharedPreferences settings = getSharedPreferences(Preferences.BOOST_PREFS, 0);
			    SharedPreferences.Editor editor = settings.edit();
			    editor.putString(Preferences.BOOST_SERVER, booST);
			    editor.putString(Preferences.KOKO_SERVER, koko);
			    editor.commit();
			    
			    booSTEndpoint.setHostName(booST);
			    kokoEndpoint.setHostName(koko);
			    
				testServers();
			}
		});
    
    	cancelButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				SharedPreferences settings = getSharedPreferences(Preferences.BOOST_PREFS, 0);
				booSTServerField.setText(settings.getString(Preferences.BOOST_SERVER, ""));
		        kokoServerField.setText(settings.getString(Preferences.KOKO_SERVER, ""));
			}
		});
    	
    	testButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				testServers();
			}
		});
    }
    
    private void testServers() {
    	
    	ImageView booSTOk = (ImageView) findViewById(R.id.booSTServerOk);
    	ImageView booSTFail = (ImageView) findViewById(R.id.booSTServerFail);
    	
    	String booSTServer = booSTServerField.getText().toString();
    	
    	if(booSTServer == null || booSTServer.equals("")) {
    		booSTOk.setVisibility(View.GONE);
    		booSTFail.setVisibility(View.GONE);
    	}
    	else if(booSTEndpoint.testServer(booSTServer)) {
    		booSTOk.setVisibility(View.VISIBLE);
    		booSTFail.setVisibility(View.GONE);
    	}
    	else {
    		booSTOk.setVisibility(View.GONE);
    		booSTFail.setVisibility(View.VISIBLE);
    	}
    }
}